function  rdPLimit(concurrency) {
  /**
   * target：对并行的promise进行并发数量的限制
   * 
   * const limit = pLimit(2);
   * const input = [
   *    limit(() => fetchSomething('foo')),
   *    limit(() => fetchSomething('bar')),
   *    limit(() => doSomething())
   * ];
   * const result = await Promise.all(input);
   * 
   */
  let active = 0;
  let queue = [];

  const resumeQueue = async (key) => {
    console.log(key, '当前进行任务数量：', active);
    if (active < concurrency && queue.length) {
      active++;
      await queue.shift()();
      active--;
      resumeQueue('后续唤起');
    }
  }


  const promiseGenerator = (cb) => {
    return new Promise(resolve => {
      const core = async () => {
        const result = await cb();
        resolve(result);
      }
      debugger;
      queue.push(core)
      // 使初始化时的resumeQueue变为微任务
      (async () => {
        await new Promise.resolve();
        resumeQueue('初始化');
      })();
    })
  }
  return promiseGenerator;
}

const limit = rdPLimit(1);
  
function asyncFun(value, delay) {
    return new Promise((resolve) => {
        console.log('start ' + value);
        setTimeout(() => resolve(value), delay);
    });
}

(async function () {
    const arr = [
        limit(() => asyncFun('aaa', 2000)),
        limit(() => asyncFun('bbb', 3000)),
    ];
  
    const result = await Promise.all(arr);
    console.log(result);
})();